var myModule = angular.module('myModule', []);

    myModule.controller('MyController', function ($scope, $http,$location) {
        
        $scope.skillTreeList=[];
        $scope.skillList=[];
        $scope.skillModifyItem={};
        $scope.skillTreeModifyItem={};

        $scope.selectedTree=null;
    	
        $scope.skillModify=function(index){
        	$scope.moSkill=$scope.skillList[index].skill_name;
        	$scope.skillModifyItem= ($scope.skillModifyItem==$scope.skillList[index] ? {} :$scope.skillList[index]);
        	
        }
        
     
        $scope.skillModifyComplete=function(index){
        	if($scope.moSkill && $scope.moSkill!=$scope.skillList[index].skill_name){
        	myUtil.onHttp($http,'post','skillModify.do',{skill_name:$scope.moSkill,skill_id:$scope.skillList[index].skill_id},null,
        			function(data){
        				$scope.skillList[index]=data;
        			}
        	)
        	}
        	else{
        		alert("기술종류명을 입력하지 않으셨거나 변경되지않았습니다.");
        	}
        }
        
        $scope.skillTreeModify=function(index){
        	$scope.moSkillTree=$scope.skillTreeList[index].skillTree_name;
        	$scope.skillTreeModifyItem= ($scope.skillTreeModifyItem==$scope.skillTreeList[index] ? {} :$scope.skillTreeList[index]);
        	
        }
        
     
        $scope.skillTreeModifyComplete=function(index){
        	if($scope.moSkillTree && $scope.moSkillTree!=$scope.skillTreeList[index].skillTree_name){
        	myUtil.onHttp($http,'post','skillTreeModify.do',{skillTree_name:$scope.moSkillTree,skillTree_id:$scope.skillTreeList[index].skillTree_id},null,
        			function(data){
        				$scope.skillTreeList[index]=data;
        			}
        	)
        	}
        	else{
        		alert("기술명종류를 입력하지 않으셨거나 변경되지않았습니다.");
        	}
        }
        
        $scope.loadData = function() {
        	myUtil.onHttp($http,'get','skillTreeList.do',null,null,
        			function(data){
        				$scope.skillTreeList=data;
        			}
        	)
 
        }
       
        
        $scope.loadSkill =function(){
        	myUtil.onHttp($http,"get",'skillList.do',{skillTree_id:$scope.selectedTree.skillTree_id},null,
        			function(data){
        				$scope.skillList=data;
        	})
        }
        
        $scope.skillAdd =function(){
        	if($scope.skill){
        	myUtil.onHttp($http,"get",'skillAdd.do',{skill_name:$scope.skill , skillTree_id:$scope.selectedTree.skillTree_id},null,
        			function(data){
        				if(!data){
        					alert("이름이 중복되어 추가할 수 없습니다.");
        				}
        				else{
        					alert("추가되었습니다.");
        					$scope.skillList.push(data);
        				}
        	})
        	}
        	else{
        		alert("기술을 입력해주세요.");
        	}
        	
        }
        
        $scope.skillTreeAdd =function(){
        	if($scope.skillTree){
        	myUtil.onHttp($http,"get",'skillTreeAdd.do',{skillTree_name:$scope.skillTree},null,
        			function(data){
        				if(!data){
        					alert("이름이 중복되어 추가할 수 없습니다.");
        				}
        				else{
        					alert("추가되었습니다.");
        					$scope.skillTreeList.push(data);
        				}
        	})
        	}
        	else{
        		alert("기술 종류를 입력해주세요.");
        	}
        	
        }
        
        
        
        $scope.loadSkill =function(){
        	myUtil.onHttp($http,"get",'skillList.do',{skillTree_id:$scope.selectedTree.skillTree_id},null,
        			function(data){
        				$scope.skillList=data;
        	})
        }
        
        $scope.skillDelete=function(index){
        	
        	if(confirm("정말 삭제하시겠습니까?")){
        	var skill_id=$scope.skillList[index].skill_id;

        	myUtil.onHttp($http,"get",'skillDelete.do',{skill_id:skill_id},null,
        			function(data){
        				if(data=='false'){
        					alert("이미 구현기술로 사용되고있기 때문에 삭제 할 수 없습니다.");
        					
        				}
        				else{
        					alert("삭제되었습니다.");
        					$scope.skillList.splice(index,1);
        				}
        	})
        	}
        }
        
        $scope.skillTreeDelete=function(index){
        	
        	if(confirm("정말 삭제하시겠습니까?")){
        	var skillTree_id=$scope.skillTreeList[index].skillTree_id;
        	
        	
        	
        	myUtil.onHttp($http,"get",'skillTreeDelete.do',{skillTree_id:skillTree_id},null,
        			function(data){
        				if(data=='false'){
        					alert("해당하는 기술들을 모두 삭제한후에 삭제가능합니다.");
        				}
        				else{
        					alert("삭제되었습니다.");
        					if($scope.skillTreeList[index]==$scope.selectedTree){
        						$scope.selectedTree=null;
        					}
        					$scope.skillTreeList.splice(index,1);
        				}
        				
        	})
        	}
        }

        
        $scope.loadData();
        
    });